package com.example.tests;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.After;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;

public class CreateFilmTest {
	
	private static WebDriver driver;
    private final String username = "admin";
    private final String password = "admin";
    private final String filmId = "03311";
    private final String filmName = "Film Film Film";
    private final String url = "http://localhost/";

    public void logIn() throws Exception { 
    	driver = new FirefoxDriver();
    	driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(url + "php4dvd/");
        WebElement usernameFind = driver.findElement(By.id("username"));
        usernameFind.clear();
        usernameFind.sendKeys(username);
        WebElement passwordFind = driver.findElement(By.name("password"));
        passwordFind.clear();
        passwordFind.sendKeys(password);
        driver.findElement(By.name("submit")).click();
    }

    @Test
    public void createFilm() throws Exception { 
	  
	    logIn();
        driver.findElement(By.cssSelector("img[alt=\"Add movie\"]")).click();
        driver.findElement(By.name("imdbid")).clear();
        driver.findElement(By.name("imdbid")).sendKeys(filmId);
        WebElement filmNameVar = driver.findElement(By.name("name"));
	    filmNameVar.clear();
        filmNameVar.sendKeys(filmName);
    
        // Assert we have what is expected, but not empty
        String filmFieldtext = filmNameVar.getAttribute("value");
        assertEquals(filmName, filmFieldtext);
        driver.findElement(By.name("year")).clear();
        driver.findElement(By.name("year")).sendKeys("1968");
        driver.findElement(By.name("duration")).clear();
        driver.findElement(By.name("duration")).sendKeys("25");
        driver.findElement(By.name("rating")).clear();
        driver.findElement(By.name("rating")).sendKeys("10");
        driver.findElement(By.name("duration")).clear();
        driver.findElement(By.name("duration")).sendKeys("19");
        driver.findElement(By.name("trailer")).clear();
        driver.findElement(By.name("trailer")).sendKeys("http://www.youtube.com/watch?v=EdwSmmXU6JM");
        driver.findElement(By.name("notes")).clear();
        driver.findElement(By.name("notes")).sendKeys("Brilliant");
        driver.findElement(By.id("text_languages_0")).clear();
        driver.findElement(By.id("text_languages_0")).sendKeys("Russian");
        driver.findElement(By.name("country")).clear();
        driver.findElement(By.name("country")).sendKeys("Russia");
        driver.findElement(By.name("producer")).clear();
        driver.findElement(By.name("producer")).sendKeys("Fyodor Khitruk");
        driver.findElement(By.name("director")).clear();
        driver.findElement(By.name("director")).sendKeys("Zorina");
        driver.findElement(By.name("writer")).clear();
        driver.findElement(By.name("writer")).sendKeys("A Snesarev");
        driver.findElement(By.name("music")).clear();
        driver.findElement(By.name("music")).sendKeys("Rock Group \"Sokol\"");
        driver.findElement(By.id("submit")).click();
    }
  
    @After
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

}
